<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="token-tag"  prefix="t"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<jsp:include page="/common/commonCss.jsp" />
<jsp:include page="/common/commonJs.jsp" />
<script type="text/javascript"
	src="${basePath}commonJs/jquery.ztree.all.js"></script>
<script type="text/javascript"
	src="${basePath}commonJs/ajaxfileupload.js"></script>
<script type="text/javascript"
	src="${basePath}commonJs/tags/My97DatePicker/WdatePicker.js"></script>
	
<script type="text/javascript">
	function getInputdispaly(pkPurordId) {//输入项 div展示
		var sbDiv = document.getElementById("inputGood");
		var sbselectOrder = document.getElementById("selectOrder");
		if (sbDiv) {
			if (sbDiv.style.display == 'none') {
				sbDiv.style.display = 'block';
				sbselectOrder.style.display = 'none';
				var trimId = $.trim(pkPurordId);
				document.getElementById("invoOderNum").value = trimId;
				document.getElementById("invoOder").value = trimId;
				document.getElementById("invoOder2").value = trimId;
				createTable();		
			} else {
				sbDiv.style.display = 'none';
				sbselectOrder.style.display = 'block';//采购列表 div展示
			}
		}
	}

	//显示新增入库中的商品列表
	function createTable()
	{
		 $.ajax({
			url:"${basePath}StockGoodsIn/findGblScmPrdEntity.shtml",
			data:$("#prdForm").serialize(),
			dataType:"json",
			type:"post",
	 		async:false,
			success:function(result)
			{
				initPrdTable(result);
			},
			error : function(XMLHttpRequest) {
				ajaxError(XMLHttpRequest);
			}
		});
	}
	
	$(document).ready(
			function() {
				//采购订单分页
				initList("addGoodsForm", "addGoodsTable", "addGoodsFoot",
						initPurTable);
			});

	function initList(formId, tableId, footId, cb) {
	
		var pkInstgIdValue =$.trim($("#pkInstgIdValue").val());
		$("#purordId").val(pkInstgIdValue);
		var select = $.trim($("#language_r").val());
		document.getElementById("deliveryStatus").value = select;
		
		var startTime =$.trim($("#startTime").val());
		$("#startSearchTime").val(startTime);
		
		var endTime =$.trim($("#endTime").val());
		$("#endSearchTime").val(endTime);
		
		var form = $("#" + formId);
		var pagenbean = new PageBean(form);
		pagenbean.setTableId(tableId);
		pagenbean.setCallback(cb);
		pagenbean.setFootId(footId);
		setPagebeanObj(pagenbean);
		pagenbean.doPage();
	}

	function initPrdList(formId, tableId, footId, cb, pkPurordId) {//商品列表分页
		var form = $("#" + formId);
		var pagenbean = new PageBean(form);
		pagenbean.setTableId(tableId);
		pagenbean.setCallback(cb);
		pagenbean.setFootId(footId);
		setPagebeanObj(pagenbean);
		pagenbean.doPage();
	}
	function initPurTable(data) {
		if (data == null) {
			return;
		}
		trimJsonArray(data); 
		$.each(data,function(ind, obj) {//显示采购订单
			var time = "";
			if(obj.applyTime != "")
			{
				var date = new Date(obj.applyTime);
				time = date.getFullYear() + "-"+ (date.getMonth() + 1) + "-"+ date.getDate();
			}
			var linkHtm = "<input type=\"button\" class=\"btn_l\" onClick=\"getInputdispaly('"
					+ obj.pkPurordId
					+ "')\" value=\"确认收货并入库\"/>";
			var delivery = "";
			var ds = obj.deliveryStatus;
			if (ds == "1") {
				delivery = "未收货";
			} else if (ds == "2") {
				delivery = "部分收货";
			} else if (ds == "3") {
				delivery = "收货完成";
				linkHtm = "";
			}
			var i = ind + 1;
			var htm = "<tr>";
			htm += "<td>" + i + "</td>";
			htm += "<td>" + obj.pkPurordId + "</td>";
			htm += "<td>" + obj.usrName + "</td>";
			htm += "<td>" + time + "</td>";
			htm += "<td>" + delivery + "</td>";
			htm += "<td>" + linkHtm + "</td></tr>";

			$("#addGoodsTable").find("tr:eq(" + ind + ")").after(htm);
		});
	}

	function initPrdTable(data) {
		if (data == null) {
			return;
		}
		$.each(data, function(ind, obj) {//显示采购订单对应的商品
			var i = ind + 1;
			var htm = "<tr>";
			htm += "<td><input type='hidden' value='"+obj.stgCnt+"'>" + i
					+ "</td>";
			htm += "<td>" + obj.pkPrdId + "</td>";
			htm += "<td>" + removeNull(obj.prdName) + "</td>";
			htm += "<td>" + removeNull(obj.cateName) + "</td>";
			htm += "<td>" + removeNull(obj.prdSpec) + "</td>";
			htm += "<td>" + removeNull(obj.unitName)+ "</td>";
			var purCnt = obj.purCnt;
			htm += "<td>" + obj.purCnt + "</td>";
			var total = obj.instgCnt == null ? 0 : obj.instgCnt;
			htm += "<td><input class=\"iput_t w100\" onchange=\"total(this,"
					+ total + "," + purCnt + ");\"></td>";
			htm += "<td>" + total + "</td>";
			htm += "<td><input class='iput_t w100'type='text' /></td></tr>";
			
			$("#prdTable").find("tr:eq(" + ind + ")").after(htm);
		});
	}

	function removeNull(args)
	{
		return (args == null ? "": args);
	}
	
	//入库或入库继续
	function stockIn(isGoOn) {
		if ($("#attFile").val() == "") {
			alertError("入库前必须上传入库单附件");
			return false;
		}
		var isOk = false;
		var content = "";
		var sum = 0;
		$("#prdTable").find("tr:not(:first)").each(function()
				{
					var num = $(this).find("td:eq(7)").find("input").val();
					num = $.trim(num);
					if (num != null && num.length != 0 ) 
					{
						//至少有一类商品填写了入库数量
						isOk = true;
					}
					if(num == "")
						{
							num = 0;
						}
					sum += num;
				});
		
			if(!isOk || sum == 0)
				{
					alertError("请至少入库一件商品");
					return false;
				}
			
			
		$("#prdTable").find("tr:not(:first)").each(
				function() {
					var id = $(this).find("td:eq(1)").html();
					var purCnt = $(this).find("td:eq(6)").html();//total
					var total = $(this).find("td:eq(8)").html();
					var num = $(this).find("td:eq(7)").find("input").val();
					var stgCnt = $(this).find("td:eq(0)").find("input").val();
					var remark = $(this).find("td:eq(9)").find("input").val();
					if(num == "")
						{
							num = 0;
						}
					//商品编号_应收数量_实际数量_累计数量_备注
					content += id + "_" + purCnt + "_" + num + "_" + stgCnt
							+ "_" + total + "_" + remark + "|";
				});
		
		if (isOk) {
			$("#content").val(content);
			$("#isGoOn").val(isGoOn);
			$("#rukuForm")[0].submit();
		}
	}

	function total(obj, total, purCnt) {//动态计算累计数量
		var v = $(obj).val();
		if($.trim(v) != "")//去空格
		{
			if (isNaN(v) || (v.indexOf(".") > -1)) {
				alertError("请输入整数");
				$(obj).val("");
				$(obj)[0].focus();
				return;
			}
		}else
			{
				v = 0;
			}
		
		if(v < 0)
		{
			alertError("请输入正数");
			$(obj).val("");
			return;
		}
		var sum = parseInt(v) + total;
		var td = $(obj).parent().parent().find("td:eq(8)");
		if (sum > purCnt) {
			alertError("当前累计数量高于应收数量！");
			//还原累计数量和应收数量
			$(obj).val("");
			td.html.val(total);
			return;
		}
		td.html(sum);
	}

	//上传附件
	function uploadAttachFile() {
		if ($("#ui-upload-input").val() == '') {
			alertError("请在上传前先选择文件");
			return false;
		}
		$.ajaxFileUpload({
			url : '${basePath}StockGoodsIn/uploadFile.shtml',
			secureuri : false, //是否启用安全提交,默认为false 
			fileElementId : 'ui-upload-input', //文件选择框的id属性
			dataType : 'text', //服务器返回的格式,可以是json或xml等
			success : function(data) { //服务器响应成功时的处理函数
				data = decodeURI(data);
				if (data != "") {
					alertInfo("入库单附件上传成功");
					$("#attFile").val(data); 

				} else {
					alertError("上传失败，请重试！");
					$("#attFile").val("");
				}
			},
			error : function(data, status, e) { //服务器响应失败时的处理函数
				alertError("上传失败，请重试！");
				$("#attFile").val("");
			}
		});
	}

	//赋值浏览上传框 显示上传的文件名
	function fileName() {
		var fileName = $("#ui-upload-input").val();
		if(fileName.lastIndexOf("\\") != -1)
			{
			fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
			}
		$("#ui-upload-filepathtxt").val(fileName);
	}
	
	//设置pageNo为1
	function select1(formId, tableId, footId, cb){
		$("#"+formId+ " input:hidden[name='pageNo']").val(1);
		initList(formId, tableId, footId, cb);
	}
</script>
<title>嘉童游乐中心-商品入库清单</title>
</head>
<body class="bg">
	<div class="content m_auto">
		<div class="content_l mt10">
			<ul>
			 <shiro:hasPermission name="StockGoodsIn/toStock_goodsIn.shtml">
       		<li class="content_hover"><a href="${basePath}StockGoodsIn/toStock_goodsIn.shtml"><i class="nav_icon_b"></i><br>商品入库</a></li>
			</shiro:hasPermission>
		<shiro:hasPermission name="StockQuery/toStockQuery.shtml">
			<li><a href="${basePath}StockQuery/toStockQuery.shtml"><i class="nav_icon_b"></i><br>库存查询</a></li>
			</shiro:hasPermission>
		<shiro:hasPermission name="StockGoodsOut/toStockGoodsOut.shtml">
			<li><a href="${basePath}StockGoodsOut/toStockGoodsOut.shtml"><i class="nav_icon_b"></i><br>商品出库</a></li>
			</shiro:hasPermission>
		<shiro:hasPermission name="StockTaking/toStockTaking.shtml">
			<li><a href="${basePath}StockTaking/toStockTaking.shtml"><i class="nav_icon_a"></i><br>库存盘点</a></li>
            </shiro:hasPermission>
		<shiro:hasPermission name="StockCorrect/toStockCorrect.shtml">
            <li><a href="${basePath}StockCorrect/toStockCorrect.shtml"><i class="nav_icon_f"></i><br>库存调整</a></li>
        </shiro:hasPermission>
        </ul>
		</div>
		<div class="content_r mt10">
			<div class="p20">
				<div class="file_single_dl f14 fixed">
					<dl class="fixed">
						<dd>
							<label><h1>商品入库清单</h1></label>
						</dd>
					</dl>
					<hr>
					<dl class="fixed">
						<dd>
							<label>入库单编号：</label> <input value="${map['stockId']}"
								readonly="readonly">
						</dd>
						<dd>
						<dd>
							<label>制单时间：</label><input name="modiTime"
								value="${map['todayTime']}" readonly="readonly">
						</dd>
						<dd>
							<label>制单人：</label><input name="modiBy"
								value="${map['userName']}" readonly="readonly">
						</dd>
					</dl>
					<hr>
					<dl class="fixed" style="overflow: hidden">
						<dd>
							<label>收货仓库：</label><input name="fkBranName"
								value="${map['storeName']}" readonly="readonly">
							<div class="uboxstyle"></div>
						</dd>
						<label><h1>入库单附件上传：</h1></label>
						<dd>
							<div class="upload_box">
								<input type="text" id="ui-upload-filepathtxt" class="iput_t fl">
								<span id="ui-upload-holder fr"> <input type="button"
									class="btn" value="上传" id="ui-upload-txt"
									onclick="uploadAttachFile();"> <input type="file"
									id="ui-upload-input" name="file" onchange="fileName()">
								</span> <input type="button" class="btn fl ml10" value="浏览">
							</div>
						</dd>
					</dl>
					<hr>
				</div>
				<div id="selectOrder">
					<div class="file_single_dl f14 fixed">
						<dl class="fixed" style="overflow: hidden">
							<dd>
								<label>订单编号：</label><input type="text" class="iput_t w200"
									id="pkInstgIdValue">
							</dd>
							<dd>
								<label>下单时间：</label><input type="text" id="startTime"
									class="Wdate iput_t w82 fl"
									onClick="WdatePicker({dateFmt:'yyyy/MM/dd',isShowWeek:true,isShowClear:true,readOnly:true})">
								<span class="mlr5 fl">至</span><input type="text" id="endTime"
									class="Wdate iput_t w82 fl"
									onClick="WdatePicker({dateFmt:'yyyy/MM/dd',isShowWeek:true,isShowClear:true,readOnly:true})">
							</dd>
							<dd>
								<label>到货状态：</label>
								<div class="uboxstyle">
									<select name="language_r" id="language_r">
										<option value="">全部</option>
										<option value="1">未收货</option>
										<option value="2">部分到货</option>
										<option value="3">收货完成</option>
									</select>
								</div>
							</dd>
							<dd class="fr">
								<input type="button" class="btn fr" value="查询"
									onclick="select1('addGoodsForm','addGoodsTable','addGoodsFoot',initPurTable);">
							</dd>
						</dl>

					</div>
					<form action="${basePath }StockGoodsIn/stockGoodsInOpertion.shtml" method="post" id="rukuForm">
						<t:token />
						<input type="hidden" name="stockId" id="stockId" value="${map['stockId']}"> 
						<input type="hidden" name="content" id="content" /> 
						<input type="hidden" name="purordId" id="invoOder2" /> 
						<input type="hidden" name="isGoOn" id="isGoOn" /> 
						<input type="hidden" name="attFile" id="attFile" />
					</form>
					<form action="${basePath }StockGoodsIn/findScmPurOrderForStock.shtml" method="post" id="addGoodsForm">
						<input type="hidden" name="pageSize" value="10"> 
						<input type="hidden" name="pageNo" value="1"> 
						<input type="hidden" name="pkPurordId" id="purordId"> 
						<input type="hidden" name="startSearchTime" id="startSearchTime">
						<input type="hidden" name="endSearchTime" id="endSearchTime">
						<input type="hidden" name="fkBranId" id="fkBranId" value="${map['storeId']}"> 
						<input type="hidden" name="deliveryStatus" id="deliveryStatus">
					</form>
					<form method="post" id="prdForm">
						<input type="hidden" id="invoOder" name="fkPurordId">
					</form>
					<table class="table" id="addGoodsTable">
						<tr>
							<th>序号</th>
							<th>采购订单编号</th>
							<th>申请人</th>
							<th>申请时间</th>
							<th>到货状态</th>
							<th>操作</th>
						</tr>
					</table>
					<input class="btn w150 mt10" type="button" value="返回商品入库列表"
						onclick="javascript:window.location.href='${basePath}StockGoodsIn/toStock_goodsIn.shtml'">
					<div class="page fr m10" id="addGoodsFoot"></div>
					<!----->
				</div>
				<div id="inputGood" style="display: none" class="mt10">
					<dl class="fixed">
						<dd>
							&nbsp;&nbsp;&nbsp;<label>订单编号：</label> <input type="text"
								readonly="readonly" id="invoOderNum" name="fkPurordId">
						</dd>
					</dl>
					<ul class="fixed" id="tab_conbox_a">
						<li class="p10 h450" style="margin-left: 10px">
							<table class="table" id="prdTable">
								<tr>
									<th>序号</th>
									<th>商品编号</th>
									<th>商品名称</th>
									<th>商品分类</th>
									<th>规格</th>
									<th>单位</th>
									<th>应收数量</th>
									<th>实际数量</th>
									<th>累计数量</th>
									<th>备注</th>
								</tr>
							</table> <!---翻页-->
							<div class="page fr m10">
								<div class="btm_btn m10">
									<dl>
										<dd>
											<input class="btn" type="button" value="入库"
												onclick="stockIn('false');">
										</dd>
										<dd>
											<input class="btn" type="button" value="入库并继续"
												onclick="stockIn('true');">
										</dd>
										<dd>
											<input class="btn" type="button" value="打印">
										</dd>
										<dd>
											<input class="btn" type="button" value="返回"
												onClick="javascript:window.location.href='${basePath}StockGoodsIn/toStockAddGoodsIn.shtml'">
										</dd>
										<dd style="width: 350px">&nbsp;</dd>
									</dl>
								</div>
							</div> <!----->

						</li>
					</ul>
				</div>
			</div>
		</div>
	</div>
</body>
</html>